package com.macaline.concurrent.demo;

import java.util.concurrent.locks.ReentrantLock;

class Ticket{
    private int number = 30;


    private ReentrantLock lock = new ReentrantLock();
    public void sale(){
        lock.lock();
        try {
            if(number > 0){
                System.out.println(Thread.currentThread().getName() + "sale第" + number-- + "张票。剩余" + number + "张");
            }
        }finally {
            lock.unlock();
        }
    }
}

public class SaleTicket {

    public static void main(String[] args) {
        Ticket ticket = new Ticket();

        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        }, "AA").start();

        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        }, "BB").start();

        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sale();
            }
        }, "CC").start();
    }

}
